package com.lrd.snapshotorder.util;


import java.time.LocalDate;
import java.time.temporal.IsoFields;
import java.time.temporal.TemporalField;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * number 生成器
 */
public class RandomCodeGenerator {

    private static final Random RANDOM = new Random();
    //private static final int NUMBER_LENGTH = 10;

    private static final String[] str = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    /**
     * 生成一个默认范围[200~500]内的随机整数
     *
     * @return
     */
    public static Integer getRangeRandomInteger(Integer min, Integer max) {
        if (min == null) {
            min = 200;
        }
        if (max == null) {
            max = 500;
        }
        // 创建一个Random对象
        Random random = new Random();
        return random.nextInt(max - min + 1) + min;
    }

    /**
     * 生成一个指定范围内的5的倍数的随机整数
     *
     * @param min
     * @param max
     * @return
     */
    public static int getRandomInteger(Integer min, Integer max) {
        Random rand = new Random();
        //生成500范围内的随机数
        int x = rand.nextInt(max + 1);
        int remainder = x % 5;
        if (remainder == 0) {
            return x;
        } else {
            int d = x + (5 - remainder);
            if (d > 500) {
                // 如果d超出了范围，则减去5
                d -= 5;
            } else if (d < min) {
                // 如果d低于范围，则加上5
                d += 5;
            }
            return d;
        }
    }

    /**
     * 返回一个有序的整数
     *
     * @return
     */
    public static String getYearAndWeek(LocalDate localDate) {
        //LocalDate currentDate = LocalDate.now();
        String year = String.valueOf(localDate.getYear()).substring(2);
        TemporalField weekField = IsoFields.WEEK_OF_WEEK_BASED_YEAR;
        int week = localDate.get(weekField);
        return year + String.format("%02d", week);
    }

    public static String generateNumber() {
        String currentDate = getCurrentDate();
        String twoDigitNumber = generateTwoDigitNumber();
        String uniqueNumber = generateUniqueNumber();
        return currentDate + twoDigitNumber + uniqueNumber;
    }

    public static String getCurrentDate() {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");
        java.util.Calendar cal = java.util.Calendar.getInstance();
        return sdf.format(cal.getTime());
    }

    public static String generateTwoDigitNumber() {
        Random random = new Random();
        int twoDigitNumber = random.nextInt(90) + 10;
        return String.valueOf(twoDigitNumber);
    }

    /**
     * 不含重复数据的组合
     *
     * @return
     */
    public static String generateUniqueNumber() {
        String uniqueNumber = "";
        for (int i = 0; i < 11; i++) {
            uniqueNumber += generateRandomDigit();
        }
        return uniqueNumber;
    }

    /**
     * 不含重复数据的组合
     *
     * @return
     */
    public static String generateNotDuplicateUniqueNumber() {
        String uniqueNumber = "";
        for (int i = 0; i < 10; i++) {
            uniqueNumber += generateRandomDigit(uniqueNumber);
        }
        return uniqueNumber;
    }

    /**
     * 不含重复数据的组合
     *
     * @return
     */
    public static String generateUniqueStr() {
        String uniqueStr = "";
        for (int i = 0; i < 3; i++) {
            uniqueStr += generateUniqueAbc(uniqueStr);
        }
        return uniqueStr;
    }

    private static String generateUniqueAbc(String uniqueStr) {
        String randomUniqueAbc = str[(RANDOM.nextInt(24))];
        if (uniqueStr.contains(randomUniqueAbc)) {
            return generateUniqueAbc(uniqueStr);
        } else {
            return randomUniqueAbc;
        }
    }

    private static String generateRandomDigit(String existingNumber) {
        String randomDigit = String.valueOf(RANDOM.nextInt(10));
        if (existingNumber.contains(randomDigit)) {
            return generateRandomDigit(existingNumber);
        } else {
            return randomDigit;
        }
    }

    private static String generateRandomDigit() {
        return String.valueOf(RANDOM.nextInt(10));
    }

    public static void main(String[] args) {
        Set<String> stringSet = new HashSet<>();
        for (int i = 0; i < 3628800; i++) {
            String number = generateUniqueNumber() + generateUniqueStr();
            System.out.println(number);
            stringSet.add(number);
        }
        System.out.println(stringSet.size());
    }
}
